// service 统一接口
import axios, { AxiosRequestConfig } from "axios";
import type { AxiosInstance } from "axios";
import type { HYRequestInterceptors, HYRequestConfig } from "./type";
import Localcahe from "@/utils/cache";
// import { ElLoading } from "element-plus";
// import { ILoadingInstance } from "element-plus/lib/el-loading/src/loading.type";

// const DEAFULT_LOADING = true;

class HYRequest {
  // showLoading: boolean;
  // loading?: ILoadingInstance;
  instance: AxiosInstance;
  // interceptors?: HYRequestInterceptors;

  constructor(config: AxiosRequestConfig) {
    // 创建axios实例
    // this.instance = axios.create(config);
    this.instance = axios.create({
      baseURL:
        process.env.NODE_ENV === "production" ? "http://8.130.32.196:1452" : "/api",
      timeout: 1000 * 180,
      withCredentials: true
    });
    // 保存基本信
    // this.showLoading = config.showLoading ?? DEAFULT_LOADING;
    // this.interceptors = config.interceptors;

    // 使用拦截器
    // 1.从config中取出的拦截器是对应的实例的拦截器
    // this.instance.interceptors.request.use(
    //   this.interceptors?.requestInterceptor,
    //   this.interceptors?.requestInterceptorCatch
    // );
    // this.instance.interceptors.response.use(
    //   this.interceptors?.responseInterceptor,
    //   this.interceptors?.responseInterceptorCatch
    // );
    // 添加所有的实例都有拦截器
    // 2.添加所有的实例都有的拦截器
    this.instance.interceptors.request.use(
      (config) => {
        console.log("所有的实例都有的拦截器: 请求成功拦截");
        // 携带token的拦截
        const token = Localcahe.getCache("token");
        if (token) {
          config.headers.Authorization = `${token}`;
        }

        return config;
      },
      (err) => {
        console.log("所有的实例都有的拦截器: 请求失败拦截");
        return err;
      }
    );

    this.instance.interceptors.response.use(
      (res) => {
        console.log("所有的实例都有的拦截器: 响应成功拦截");
        return res;
      },
      (err) => {
        console.log("所有的实例都有的拦截器: 响应失败拦截");
        console.log(err.response);

        return err;
      }
    );
  }

  request<T>(config: HYRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      // 1.单个请求对请求config的处理
      if (config.interceptors?.requestInterceptor) {
        config = config.interceptors.requestInterceptor(config);
      }

      this.instance
        .request<any, T>(config)
        .then((res) => {
          // 1.单个请求对数据的处理
          if (config.interceptors?.responseInterceptor) {
            res = config.interceptors.responseInterceptor(res);
          }

          // 3.将结果resolve返回出去
          resolve(res);
        })
        .catch((err) => {
          reject(err);
          return err;
        });
    });
  }

  get<T>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: "GET" });
  }

  post<T>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: "POST" });
  }

  delete<T>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: "DELETE" });
  }

  patch<T>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: "PATCH" });
  }
  put<T>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: "PUT" });
  }
}
export default HYRequest;
